package com.park.service.impl.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.client.model.user.UserModel;
import com.park.client.service.user.UserService;
import com.park.common.NameHelper;
import com.park.common.file.ImageUtil;
import com.park.common.id.IdFactoryUtils;
import com.park.common.util.MD5;
import com.park.common.util.Result;
import com.park.dao.user.UserMapper;
import com.park.service.impl.base.BaseServiceImpl;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<UserModel> implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public UserModel login(String username) {
		return userMapper.login(username);
	}

	@Override
	public boolean updatePwd(UserModel user) {
		return userMapper.updatePwd(user);
	}

	@Override
	public List<UserModel> selectLastRegister() {
		return userMapper.selectLastRegister();
	}

	@Override
	public boolean uploadPicture(UserModel user) {
		return userMapper.updateByPrimaryKey(user) == 1;
	}

	@Override
	public boolean updateAccountStatus(String id) {
		return false;
	}

	@Override
	public UserModel selectByEmail(String email) {
		return null;
	}

	@Override
	public List<UserModel> selectIntegralRanking() {
		return userMapper.selectIntegralRanking();
	}

	@Override
	public List<UserModel> selectByList(List<UserModel> list) {
		return userMapper.selectByList(list);
	}

	@Override
	public UserModel registerCheck(UserModel user) {
		return userMapper.registerCheck(user);
	}
	
	@Override
	public void saveOrUpdate(UserModel user) {
		if(user.getId() == null){
			user.setId(IdFactoryUtils.getId());
			this.userMapper.insertSelective(user);
		}else{
			this.userMapper.updateByPrimaryKeySelective(user);
		}
	}

	@Override
	public Result<Boolean> registerUser(UserModel user) {
		Result<Boolean> result = new Result<>(true);
		try {
			user.setPassword(MD5.md5(user.getPassword()));
			user.setId(IdFactoryUtils.getId());
			user.setCreateDate(new Date());
			user.setChName(NameHelper.chName());
			user.setPicture(ImageUtil.getImageBytes());
			this.userMapper.insertSelective(user);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("注册失败");
		}
		return result;
	}


	
}
